# -*- encoding=utf8 -*-
__author__ = "chen"
import random
from common.common_util import *
def jingjisai():
    try:
        # pcos = wait(Template(r"tpl1711375856772.png", record_pos=(0.269, 0.204), resolution=(629, 499)))
        pcos = wait(Template(r"tpl1726064610658.png", record_pos=(0.355, 0.215), resolution=(655, 518)),1)
        x = random.randint(-10, 10)
        y = random.randint(-2, 2)
        sleep(2)
        touch((pcos[0] + x, pcos[1] + y))
        return True
    except :
        print("xxxxx")
        return False
def clik_auto():
    try:
        wait(Template(r"tpl1711549675033.png", record_pos=(0.464, 0.265), resolution=(635, 503)), 1)
        pcos = wait(Template(r"tpl1711375898773.png", record_pos=(0.459, 0.319), resolution=(629, 499)),1)
        x = random.randint(-10, 10)
        y = random.randint(-2, 2)
        sleep(2)
        touch((pcos[0] + x, pcos[1] + y))
    except :
        print("xxxxx")
def get_task_hwnds(ex_hwns_id):
    hwnds = get_all_windows("梦幻西游")
    for i in range(len(hwnds)):
        title = win32gui.GetWindowText(hwnds[i])
        if f"[{ex_hwns_id}]" in title:
            return hwnds[i]
def run_jiqichang(ex_hwns_ids,counter):
    js_data =[]
    if len(ex_hwns_ids) !=0:
        for id in ex_hwns_ids:
            hwnd = get_task_hwnds(id)
            if hwnd:
                js_data.append([win32gui.GetWindowText(hwnd),hwnd,0]) #设备编号，句柄值，次数
    else:
        hwnds = get_all_windows("梦幻西游")
        for i in range(len(hwnds)):
            js_data.append([win32gui.GetWindowText(hwnds[i]), hwnds[i], 0])  # 设备编号，句柄值，次数

    print(js_data)
    for i in range(1000):
        for j in range(len(js_data)):
            connect_device(f"Windows:///{js_data[j][1]}")
            if js_data[j][2] < 10:
                fl = jingjisai()
                clik_auto()
                if fl:
                    js_data[j][2]  = js_data[j][2] +1
        bl = True
        for i in range(len(js_data)):
            bl1 = js_data[i][2] == counter
            bl = (bl and bl1)
        if bl:
            break
        print(js_data)

if __name__ == '__main__':
    # 为空时，全部执行
    counter = 10  # 执行次数
    ex_hwns_ids = []
    run_jiqichang(ex_hwns_ids, counter)




